# @FileName  : file.py
# @Time      : 2025/3/6 08:54
# @Author    : LuZhaoHui
# @Software  : PyCharm
from vars import *
from dt import now_date_str, now_time_str
import os
from PyQt5.QtCore import *
import hashlib, json
from PIL import Image

A3 = (3508, 4961)
A4 = (2481, 3508)
A5 = (1749, 2481)

DEFAULT_DPI = (300.0, 300.0)


def delete_file(name):
    if os.path.exists(name):
        os.remove(name)


def get_list_dir(dirs):
    # msg = eval(dirs)
    msg = dirs.split(',')
    if isinstance(msg, str):
        if os.path.exists(msg):
            return msg
    else:
        for s in msg:
            if os.path.exists(s):
                return s
        return ''


def get_path1(path):
    return os.path.dirname(os.path.dirname(path))


def get_path2(path):
    return os.path.dirname(get_path1(path))


def path_join(path1, path2):
    return os.path.join(path1, path2).replace('\\', '/')


def ret_dir_list(path):
    dirs = []
    if os.path.exists(path):
        for f in sorted(os.listdir(path)):
            if os.path.isdir(path_join(path, f)):
                dirs.append(f)
    return dirs


def get_file_byte(file):
    info = QFileInfo(file)
    byte = info.size()
    return byte


def get_file_kbyte(file):
    info = QFileInfo(file)
    byte = info.size()
    return int(byte / 1024)


def get_file_byte_modify(file):
    info = QFileInfo(file)
    byte = info.size()
    modify = info.lastModified()
    return byte, modify


def get_file_md5(file):
    md5 = ''
    size = 64 * 1024
    if os.path.isfile(file):
        with open(file, 'rb') as f:
            md5_obj = hashlib.md5()
            while True:
                d = f.read(size)
                if not d:
                    break
                md5_obj.update(d)
            hash_code = md5_obj.hexdigest()
            md5 = str(hash_code).lower()
    return md5


def get_file_encrypt(file, encrypt_type='sha1'):
    sha = ''
    if os.path.isfile(file):
        with open(file, 'rb') as f:
            hash_code = hashlib.new(encrypt_type, f.read()).hexdigest()
            sha = str(hash_code).lower()
    return sha


def read_json(file):
    with open(file, 'r', encoding='utf-8') as f:
        json_str = f.read()
        f.close()
    return json.loads(json_str)


def save_json(save_dict, file, mode='w'):
    try:
        with open(file, mode, encoding='utf-8') as f:
            json_str = json.dumps(save_dict, ensure_ascii=False)
            f.write(json_str)
    except Exception as e:
        print(e)


def save_msg(msg, file):
    try:
        with open(file, 'a', encoding='utf-8') as f:
            f.write(msg)
    except Exception as e:
        print(e)


def save_log(msg, log=''):
    if not os.path.exists(LOG_PATH):
        os.mkdir(LOG_PATH)
    if len(log) == 0:
        file = LOG_PATH + '/' + now_date_str() + '.log'
    else:
        if len(os.path.splitext(log)) > 1:
            file = LOG_PATH + '/' + log
        else:
            file = LOG_PATH + '/' + log + '-' + now_date_str() + '.log'
    log_str = ''
    try:
        with open(file, 'a', encoding='utf-8') as f:
            log_str = now_time_str() + ' ' + msg + '\n'
            f.write(log_str)
    except Exception as e:
        print(e)
    return log_str


def getImg1File(file):
    ret = []
    try:
        image = Image.open(file)
        ret = [image.size[0], image.size[1],
               int(image.info['dpi'][0]), int(image.info['dpi'][1]),
               image.mode]
    except Exception:
        ret = [0, 0, 300, 300, ' ']
    return ret

def getImg2File(file):
    try:
        image = Image.open(file)
    except Exception:
        return [0, 0, 300, 300, ' ']
    dpi = DEFAULT_DPI
    if 'dpi' in image.info.keys():
        dpi = image.info['dpi']
    return [image.size[0], image.size[1], int(dpi[0]), int(dpi[1]), image.mode]
